
package abc;
public class Arrylist implements List {

    private Object[] data;
    private int len;

    public Arrylist() {
        this.data = new Object[1000];
        this.len = 0;
    }

  
    public void clear() {
        this.len = 0;
    }

   
    public boolean isEmpty() {
        return this.len == 0;
    }

 
    public int length() {
        return this.len;
    }

   
    public Object get(int index) {
        if (index < 0 || index > this.len) {
            throw new ArrayIndexOutOfBoundsException("下标越界");
        }
        return this.data[index-1 ];
    }

    
    public int locate(Object o) {
        for (int i = 0; i < this.len; i++) {
            if (o==(this.data[i])) {
                return i + 1;
            }
        }
        return -1;
    }

 
    public Object prior(Object o) {
        Object m=0;
        if(this.len==0)
            throw new UnsupportedOperationException("Not supported yet."); 
        if(this.len>0&&this.data[0]==o)
             System.out.println("数组第一位数无前继.");
        for(int index=1;index<this.len;index++){
            if(this.data[index]==o)
                System.out.println("这个数的前继有："+this.data[index-1]);
        }
        return m;
    }
    

   
    public Object next(Object o) {
        Object m=0;
        if(this.len==0)
            throw new UnsupportedOperationException("Not supported yet."); 
        for(int index=0;index<=this.len-2;index++){
            if(this.data[index]==o)
                System.out.println("这个数的后继有"+this.data[index+1]);
        }
        if(this.len>0&&this.data[this.len-1]==o)
            System.out.println("数组最后一个数没有后继.");
        return m;
    }

   
    public void insert(int index,Object o) {
        // 检查参数
        // 有没有满
        if (this.len == 1000) {
            throw new ArrayIndexOutOfBoundsException("数组已满");
        }
        //检查index越界
        if (index < 0 || index > this.len) {
            throw new ArrayIndexOutOfBoundsException("下标越界");
        }

        int index1 = index - 1;

        int pos = this.len - 1;
        for (; pos > index1; pos--) {
            this.data[pos + 1] = this.data[pos];
        }
        data[pos + 1] = o;
        this.len++;

    }

   
    public Object delete(int index) {
        Object m;
        if(index<1||index>this.len){
            throw new ArrayIndexOutOfBoundsException("下标越界");
        }
        else{
        int index1=index-1;
        int len1=this.len-1;
        m=this.data[index-1];
        for(;index1<len1;index1++)
            this.data[index1]=this.data[index1+1];
        this.len--;
        return m;
        }
        
    }

   
    public void traverse() {
        for (int i = 0; i < this.len; i++) {
            System.out.print(this.data[i] + "  ");
        }
    }

    
}
